package com.fiberhome.ztelauncher.util;


import com.fiberhome.ztelauncher.xml.bean.LaunchDataCommon;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataPage;
import com.fiberhome.ztelauncher.xml.bean.LauncherLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by xc on 2018/3/22.
 * 描述：
 */

public class DataUtil {

    /**
     * 页面数据排序
     * @param list
     * @param listLayout
     * @return
     */
    public static List<LaunchDataPage.Element> sortPageData(List<LaunchDataPage.Element> list,
                                                            List<LauncherLayout.Element> listLayout) {
        List<LaunchDataPage.Element> newList = new ArrayList<LaunchDataPage.Element>();
        if (list == null || listLayout == null) {
            return newList;
        }
        for (int i = 0; i < listLayout.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (listLayout.get(i).id.equals(list.get(j).id)) {
                    newList.add(list.get(j));
                }
            }
        }
        return newList;
    }

    /**
     * 布局排序
     *
     * @param list
     */
    public static List<LauncherLayout.Element> sortPageLayout(List<LauncherLayout.Element> list) {
        Collections.sort(list, new Comparator<LauncherLayout.Element>() {
            @Override
            public int compare(LauncherLayout.Element element, LauncherLayout.Element t1) {
                return element.left * 100 + element.top - (t1.left * 100 + t1.top);
            }
        });
        return list;
    }

    /**
     * 导航栏排序
     */
    public static List<LaunchDataCommon.Nav> sortNavs(List<LaunchDataCommon.Nav> navList,
                                                      List<LauncherLayout.Page> pageList) {
        List<LaunchDataCommon.Nav> newList = new ArrayList<LaunchDataCommon.Nav>();
        if (navList == null || pageList == null) {
            return newList;
        }
        for (int i = 0; i < pageList.size(); i++) {
            for (int j = 0; j < navList.size(); j++) {
                if (pageList.get(i).id.equals(navList.get(j).id)) {
                    newList.add(navList.get(j));
                }
            }
        }
        return newList;
    }
}
